﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DBLibrary;

namespace WPFClient
{
    /// <summary>
    /// Logique d'interaction pour Register.xaml
    /// </summary>
    public partial class Register : Window
    {
        private Customer customer;

        public Register()
        {
            InitializeComponent();

            customer = new Customer();
        }

        private void buttonRegister_Click(object sender, RoutedEventArgs e)
        {
            String login = textBoxLogin.Text;
            String password = textBoxPassword.Password;

            // ensure that both password and confirm field are equals
            //
            if (password != textBoxConfirm.Password)
            {
                MessageBox.Show("La vérification de mot de passe a échouée. Vérifiez qu'ils soient bien égaux.",
                    "Mots de passes différents", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            // Try to register the customer
            //
            try
            {
                customer.addCustomer(textBoxLogin.Text, textBoxPassword.Password, false);
                customer.connect(login, password);
                if (customer.IsConnected)
                {
                    MainWindow main = new MainWindow(customer);
                    App.Current.MainWindow = main;
                    this.Close();
                    main.Show();
                }
                else MessageBox.Show("Une erreur est survenue lors de la tentative de connexion. Réessayez plus tard.",
                    "Connexion impossible", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (InsertException exception)
            {
                MessageBox.Show(exception.Message, "Erreur d'inscription", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
